<template>
  <!-- 添加或修改工单信息对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="采购方总部编码" prop="purchaserHqCode">
                <el-input v-model="form.purchaserHqCode" placeholder="请输入采购方总部编码"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="生产订单编号" prop="ipoNo">
                <el-input v-model="form.ipoNo" placeholder="请输入供应商生产订单编号" disabled/>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="产品选择" prop="goodsSaleId">
                <el-select v-model="form.goodsSaleId" placeholder="请选择产品" filterable clearable style="width: 100%">
                  <el-option v-for="item in goodsList"
                             :key="item.saleId" :label="item.name" :value="item.saleId"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="生产工单编码" prop="woNo">
                <el-input v-model="form.woNo" placeholder="请输入生产工单编码"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="品类编码" prop="categoryCode">
                <el-input v-model="form.categoryCode" placeholder="请输入品类编码" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="种类编码" prop="subclassCode">
                <el-input v-model="form.subclassCode" placeholder="请输入种类编码" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="厂家物料编码" prop="materialsCode">
                <el-input v-model="form.materialsCode" placeholder="请输入厂家物料编码"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="厂家物料描述" prop="materialsDescription">
                <el-input v-model="form.materialsDescription" placeholder="请输入厂家物料描述" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="物料批次" prop="materialsBatch">
                <el-input v-model="form.materialsBatch" placeholder="请输入物料批次"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="生产数量" prop="amount">
                <el-input v-model="form.amount" placeholder="请输入生产数量"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="计量单位" prop="unit">
                <el-input v-model="form.unit" placeholder="请输入计量单位"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="计划开始日期" prop="planStartDate">
                <el-date-picker clearable
                                v-model="form.planStartDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请输入计划开始日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="计划完成日期" prop="planFinishDate">
                <el-date-picker clearable
                                v-model="form.planFinishDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请输入计划完成日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实际开始日期" prop="actualStartDate">
                <el-date-picker clearable
                                v-model="form.actualStartDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请输入实际开始日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实际完成日期" prop="actualFinishDate">
                <el-date-picker clearable
                                v-model="form.actualFinishDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请输入实际完成日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <!--<el-col :span="12">
                <el-form-item label="实物ID" prop="entityCode">
                    <el-input v-model="form.entityCode" placeholder="请输入实物ID"/>
                </el-form-item>
            </el-col>-->
            <el-col :span="12">
              <el-form-item label="工艺路线编码" prop="processRouteNo">
                <el-input v-model="form.processRouteNo" placeholder="请输入工艺路线编码"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电压等级" prop="voltageLevel">
                <el-input v-model="form.voltageLevel" placeholder="请输入电压等级"/>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="子种类编号" prop="subcategoryCode">
                <el-input v-model="form.subcategoryCode" placeholder="请输入子种类编号"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="物资子种类" prop="subcategoryName">
                <el-input v-model="form.subcategoryName" placeholder="请输入物资子种类"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电抗率" prop="reactanceRatio">
                <el-input v-model="form.reactanceRatio" placeholder="请输入电抗率"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAction, getAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';

export default {
  name: "FactoryProduceWorkForm",
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsSaleId: [
          {required: true, message: "产品信息不能为空", trigger: "blur"}
        ],
      },
      url: {
        getById: '/factoryProduceWork/factoryProduceWork/',
        add: '/factoryProduceWork/factoryProduceWork/add',
        edit: '/factoryProduceWork/factoryProduceWork/edit',
        goodsList: '/factoryProduceSale/factoryProduceSale/list',
      },
      goodsList: []
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: "0",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        purchaserHqCode: null,
        ipoNo: null,
        supplierCode: null,
        supplierName: null,
        woNo: null,
        categoryCode: "60",
        subclassCode: "60004",
        materialsCode: null,
        materialsDescription: null,
        materialsBatch: null,
        amount: null,
        unit: null,
        planStartDate: null,
        planFinishDate: null,
        actualStartDate: null,
        actualFinishDate: null,
        woStatus: "0",
        entityCode: null,
        processRouteNo: null,
        voltageLevel: null,
        dataSource: null,
        dataSourceCreateTime: null,
        ownerId: null,
        openId: null,
        modelSpec: null,
        subcategoryCode: null,
        subcategoryName: null,
        productName: null,
        reactanceRatio: null,
        projectId: null,
        proNo: null,
        planNo: null,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd(produceId,ipoNo) {
      this.reset();
      const now = new Date();
      const year = now.getFullYear(); // 年份，例如：2021
      const month = now.getMonth() + 1; // 月份，范围是0-11，需要+1，例如：1月为1
      const day = now.getDate(); // 日期，例如：31
      const hours = now.getHours(); // 小时，24小时制，例如：23
      const minutes = now.getMinutes(); // 分钟，例如：59
      const seconds = now.getSeconds(); // 秒，例如：30
      this.form.woNo = "GONG" + year + month + day + hours + minutes + seconds;
      this.form.ipoNo = ipoNo;
      this.form.produceId = produceId;
      this.open = true;
      this.title = "添加工单信息";
      this.getGoodsList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工单信息";
        this.getGoodsList();
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },

    getGoodsList() {
      let projectId = this.form.projectId;
      let produceId = this.form.produceId;
      getAction(this.url.goodsList, {"produceId": produceId}).then(response => {
        this.goodsList = response.rows;
      });
    }

  }
};
</script>
